#/usr/bin/perl                                                                                                                           

%patterns;

# Clone size distrib hash
# The hash key is the size of the clone
# the value is the number of clones of that size
%clone_size;

for $sw (1..1){

    # empty the size distrib hash
    %clone_size=();

    %patterns=();
    # Important = dead crypts have cloneId=-1, so include cloneId=-1 in patterns
    # the pattern of a dead crypt is wild type...
    # Try hashes of arrays to prevent memory explosions
    my @temp;
    for my $loc (0..243){
	$temp[$loc]=100;
    }
    # load up an array
    $patterns{-1}=[@temp];
    # Load all patterns in memory
    open (in,"sweep$sw/sweep$sw.patterns");
    while(<in>){
	chomp;
	@row=split(/\,/);
	# clone_id, locus 0..244, ms length = "pattern"	
	for my $loc (0..243){
	    # Add to an array
	    push @{ $patterns{$row[0]} }, $row[$loc+1]; 
	    #$patterns{$row[0]}{$loc}=$row[$loc+1];
	}
    }
    close in;

    my $day = 182; 
    my $pattern_index = 0;
    # Store ms pattern at each time step 
    %pathash=();
    %cidhash=();
    %cid_pat=(); # key is clone id, value is pattern
    %patsize=(); 

    open (i1,"sweep$sw/sweep$sw.log");
    open (out, ">sweep$sw/sweep$sw.unique.clones.size.distrib.log");
    while(<i1>){
	chomp;
	@row=split(/\,/);
	if($row[2]!=$day){
	    
	    $sizeofpathash = keys(%pathash);
	    #print out "$day,$sizeofpathash\n";
	    while (($key, $value) = each(%patsize)) {
		print out "$day,$value\n";
	    }
	    #print out "$day,$sizeofpathash\n";
	    %pathash = ();
	    %cidhash = ();
	    %cid_pat=(); # key is clone id, value is pattern
	    %patsize=(); 
	    $pattern_index = 0;

	    # increase day
	    if($day==7280){
		$day=7300;
	    } else {
		$day+=182;
	    }
	} else {
	    # Add ms pattern to hash
	    $pat = "";
	    $cid = $row[1];
	    # ignore counting the dead clone pattern
	    if($cid!=-1){
		if(!defined($cidhash{$cid})){
		    $cidhash{$cid} = 1;
		    # go through microsats and add them to hash
		    for my $loc (0..243){
			$pat=$pat.$patterns{$cid}[$loc];
		    }
		    # Add pattern to hash, but 2 different clones with same pattern
		    # get added to the hash at the same place...		    
		    
		    # If the pattern is the same, add
		    if(!defined($pathash{$pat})){
			$pattern_index++;
			$pathash{$pat}=$pattern_index; # every unique pattern has an unique index
			$cid_pat{$cid}=$pathash{$pat};
		    } else {		       
			#$pathash{$pat}=;
			$cid_pat{$cid}=$pathash{$pat};
		    }		    
		    if(!defined($patsize{$cid_pat{$cid}})){
			$patsize{$cid_pat{$cid}}=1;
		    } else {
			$patsize{$cid_pat{$cid}}++;
		    }
		} else {
		    # The clone is already present in the hash
		    #$cidhash{$cid}++; # increase the count of clones with this particular cid
		    # Get the pattern index to which the cid corresponds
		    $patsize{$cid_pat{$cid}}++;
		}
	    }
	}
    }
    close i1;    

    # For day 7300
    #$sizeofpathash = keys(%pathash);
    #print out "$day,$sizeofpathash\n";
    while (($key, $value) = each(%patsize)) {
	print out "$day,$value\n";
    }
    close out;    
}

    
